package tedu.shoot;
import java.awt.image.BufferedImage;
import java.util.Random;

public abstract class FlyingObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    public static final int REMOVE = 2;
    protected int state = LIVE;


    protected int width;
    protected int height;
    protected int x;
    protected int y;
    public FlyingObject(int width, int height){
        this.width = width;
        this.height = height;
        Random rand = new Random();
        x = rand.nextInt(World.WIDTH-width);
        y = -height;
    }

    public FlyingObject(int width, int height, int x, int y){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }

    /** 获取对象的图片 */
    public abstract BufferedImage getImage();

    /** 判断状态 */
    public boolean isLive(){
        return state == LIVE;
    }

    public boolean isDead(){
        return state == DEAD;
    }

    public boolean isRemove(){
        return state == REMOVE;
    }

    /** 移动方法抽象 */
    public abstract void step();

    /** 越界 */
    public boolean isOutOfBounds(){
        return y >= World.HEIGHT;
    }

    /** 碰撞 */
    public boolean isHit(FlyingObject other){
        int x1 = this.x - other.width;
        int x2 = this.x + this.width;
        int y1 = this.y - other.height;
        int y2 = this.y + this.height;
        int x = other.x;
        int y = other.y;
        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
    }

    public void goDead(){
        state = DEAD;
    }
}
